class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;

	TreeNode() {
	}

	TreeNode(int val) {
		this.val = val;
	}

	TreeNode(int val, TreeNode left, TreeNode right) {
		this.val = val;
		this.left = left;
		this.right = right;
	}
}

public class Solution01 {
	private int sum;

	public TreeNode convertBST(TreeNode root) {
		dfs(root);
		return root;
	}

	private void dfs(TreeNode root) {
		if (root != null) {
			dfs(root.right);
			root.val += sum;
			sum = root.val;
			dfs(root.left);
		}
	}
}
